﻿// Copyright 2012 Jack Bulan (agentgeo)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using GemWorkshop.Formats.Nitro;

namespace GemWorkshop.Map
{
    public class MatrixLoader
    {
        private Narc narc;
        public Header header;
        public ushort[,] Layer3; // always
        public byte[,] Layer2; // if unknownlayer == 1
        public ushort[,] Layer1; // if layers == 2

        public MatrixLoader(Narc matNarc)
        {
            narc = matNarc;
        }

        public void Load(int num)
        {
            BinaryReader br = new BinaryReader(narc.ExtractStream((uint)num));

            // Load the header
            header = new Header();
            header.Width = br.ReadByte();
            header.Height = br.ReadByte();
            //header.HasMapHeaders = (br.ReadByte() == 0);
            //header.HasBorderMapHeight = (br.ReadByte() == 0);
            header.UnknownLayer = br.ReadByte();
            header.Layers = (byte)(br.ReadByte() + 1);
            byte len = br.ReadByte();
            header.Name = Encoding.UTF8.GetString(br.ReadBytes(len));

            // Map files
            Layer1 = new ushort[header.Width, header.Height];
            for (int y = 0; y < header.Height; y++)
            {
                for (int x = 0; x < header.Width; x++)
                {
                    Layer1[x, y] = br.ReadUInt16();
                }
            }

            // Map border heights
            if (header.UnknownLayer == 1)
            {
                Layer2 = new byte[header.Width, header.Height];
                for (int y = 0; y < header.Height; y++)
                {
                    for (int x = 0; x < header.Width; x++)
                    {
                        Layer2[x, y] = br.ReadByte();
                    }
                }
            }
            else Layer2 = null;

            // Map headers
            if (header.Layers == 2)
            {
                Layer3 = new ushort[header.Width, header.Height];
                for (int y = 0; y < header.Height; y++)
                {
                    for (int x = 0; x < header.Width; x++)
                    {
                        Layer3[x, y] = br.ReadUInt16();
                    }
                }
            }
            else Layer3 = null;

            br.Close();
            br.Dispose();
        }
        
        public struct Header
        {
            public byte Width, Height;
            public byte UnknownLayer, Layers; // + 1
            public string Name;
        }
    }
}
